package models

import (
	"github.com/beego/beego/v2/client/orm"
)

type CustomerViewModel struct {
	CustomerID             int    `json:"customerID" orm:"column(customer_ID)"`
	CustomerName           string `json:"customerName" orm:"column(customer_name)"`
	CustomerCompany        string `json:"customerCompany" orm:"column(company_name)"`
	CustomerEmail          string `json:"customerEmail" orm:"column(customer_email)"`
	CustomerPhone          string `json:"customerPhone" orm:"column(customer_phone)"`
	ServiceTimes           int    `json:"serviceTimes" orm:"column(service_times)"`
	CustomerLastFinishTime string `json:"customerLastFinishTime" orm:"column(last_service_finish_time)"`
	ServiceStatus          int    `json:"serviceStatus" orm:"column(service_status)"`
}

// GetCustomerViewInformationByCustomerID 查询客户详细信息
func GetCustomerViewInformationByCustomerID(customerID int) (CustomerViewModel, error) {
	o := orm.NewOrm()
	var customer CustomerViewModel
	err := o.Raw("Select cus.customer_ID,cus.customer_name, cus.company_name,cus.customer_email,cus.customer_phone, cus.service_times, cus.last_service_finish_time, ser.service_status FROM contact_customer_mid ccm LEFT JOIN  contacts con on ccm.contact_ID = con.contact_ID LEFT JOIN customers cus on ccm.customer_ID = cus.customer_ID LEFT JOIN services ser on ser.customer_ID = cus.customer_ID where cus.customer_ID = ?", customerID).QueryRow(&customer)
	if err == nil {
		return customer, nil
	} else {
		return CustomerViewModel{}, err
	}
}
